import os
import re

def parse_proxy_config():
    config = None
    try:
        config = open('client.ini')
        for line in config:
            if len(line.strip()) == 0: continue
            key, value = re.split(r'\s*=\s*', line.strip())
            if key == 'proxy':
                host, port = value.split(':')
                port = int(port)
                return (host, port)                 
    finally:
        if config: config.close()
        
def parse_watch_dir():
    config = None
    try:
        config = open('client.ini')
        dir = []
        for line in config:
            if len(line.strip()) == 0: continue
            key, value = re.split(r'\s*=\s*', line.strip())
            if key != 'proxy':
                dir.append((value, key))
        return dir
    finally:
        if config: config.close()     

def parse_filter_rule():
    config = None
    try:
        config = open('filter.ini')
        include = []
        exclude = []
        for line in config:
            if len(line.strip()) == 0 or line[0] == '#': continue
            key, value = re.split(r'\s*=\s*', line.strip())
            if key == 'include':
                alist = [i.strip() for i in re.split(r'[\s,:;|]', value)]
                include.extend(alist)
            elif key == 'exclude':
                alist = [i.strip() for i in re.split(r'[\s,:;|]', value)]                
                exclude.extend(alist)
        return set(include), set(exclude)
    finally:
        if config: config.close()     


def memorize(func, cache={}):
    def gfunc(*args, **kwargs):
        key = (func, tuple(args), frozenset(kwargs.items()))
        if key not in cache:
            cache[key] = func(*args, **kwargs)
        return cache[key]
    return gfunc

@memorize
def get_remote_dir(sync_id):
    config = None
    try:
        config = open('proxy.ini')
        for line in config:
            line = line.strip()
            print line
            if len(line) == 0 or line[0] == '#': continue
            key, value = re.split(r'\s*=\s*', line)
            if key == sync_id:
                return value
    finally:
        if config: config.close()

@memorize
def get_user_password(username):
    config = None
    try:
        mykey = 'password.' + username
        config = open('proxy.ini')
        for line in config:
            line = line.strip()
            if len(line) == 0 or line[0] == '#': continue
            key, value = re.split(r'\s*=\s*', line)
            if key == mykey:
                return value
    finally:
        if config: config.close()


def get_bind_address():
    config = None
    try:
        config = open('proxy.ini')
        for line in config:
            line = line.strip()
            print line
            if len(line) == 0 or line[0] == '#': continue
            key, value = re.split(r'\s*=\s*', line)
            if key == 'bind_address':
                host, port = value.split(':')
                port = int(port)
                return (host, port)                  
    finally:
        if config: config.close()

if __name__ == '__main__':
    config_path = os.path.dirname(os.path.abspath(__file__))
    os.chdir(config_path)
    print parse_proxy_config()
    print parse_watch_dir()
    print parse_filter_rule()
    print get_remote_dir('colinli_service')
    print get_user_password('mqq')
    print get_bind_address()
    
